﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Model.FromBody;
using CoreShop.Services;
using grproLib;
using Newtonsoft.Json;
using SqlSugar;
using SqlSugar.Extensions;
using Sunny.UI;

namespace CoreShop.PrintService
{
    public class ReportServices
    {
        protected GridppReport Report = new GridppReport();
        protected GridppReport SubPayment = new GridppReport();//合计付款方式
        protected GridppReport SubPresent = new GridppReport();//合计付款方式
        protected GridppReport SubFoodDepart = new GridppReport();//菜品分类

        private List<corecmsprinttaskdetail> taskdetails = new List<corecmsprinttaskdetail>();
        private corecmsuser corecmsuser = null;
        private corecmsfoodordermaster orderMaster = null;
        private corecmsfoodorderpayment orderPayment = null;
        private List<corecmsfoodorderpayment> paymentList = null;
        private List<ClassDeparts> departList = null;
        private corecmstakeoutdetail takeoutdetail = null;

        //在C#中一次填入一条记录不能成功，只能使用一次将记录全部填充完的方式
        private void ReportFetchRecord()
        {
            try
            {
                Report.DetailGrid.Recordset.Empty();
                if ((taskdetails == null) || (taskdetails.Count == 0)) return;
                var taskdetail1 = taskdetails.First();
                PropertyInfo[] properties1 = taskdetail1.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

                orderMaster.sPresent = "";//是否有赠送
                var dataDetails = taskdetails;
                if (orderMaster.isPresent)
                {
                    dataDetails = taskdetails.Where(p => p.dataType != (int)GlobalEnumVars.OrderingDataType.Present).ToList();
                    if (taskdetails.Count > dataDetails.Count) orderMaster.sPresent = "是";
                }
                foreach (var taskdetail in dataDetails)
                {
                    if ((string.IsNullOrEmpty(taskdetail.deskName)) && (orderMaster != null)) taskdetail.deskName = orderMaster.deskName;
                    if (taskdetail.callType == ((int)GlobalEnumVars.CallType.Await).ToString())
                        taskdetail.callType = "叫起";
                    else taskdetail.callType = "";
                    Report.DetailGrid.Recordset.Append();
                    #region 菜品明细与Grid++Report记录集的字段之间的对应关系
                    foreach (PropertyInfo item1 in properties1)
                    {
                        string name = item1.Name;
                        object value = item1.GetValue(taskdetail, null);

                        foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                        {
                            if (String.Compare(fld.RunningDBField, name, true) == 0)
                            {
                                fld.Value = value;
                                break;
                            }
                        }
                    }
                    #endregion

                    #region 会员资料与Grid++Report记录集的字段之间的对应关系
                    if (corecmsuser != null)
                    {
                        PropertyInfo[] properties2 = corecmsuser.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        foreach (PropertyInfo item2 in properties2)
                        {
                            string name = item2.Name;
                            object value = item2.GetValue(corecmsuser, null);

                            foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                            {
                                if ((String.Compare(fld.RunningDBField, name, true) == 0) && (!name.Equals("quantity", StringComparison.OrdinalIgnoreCase)))
                                {
                                    fld.Value = value;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion

                    #region 主表Master与Grid++Report记录集的字段之间的对应关系
                    if (orderMaster != null)
                    {
                        orderMaster.deskName = taskdetail.deskName;
                        PropertyInfo[] properties0 = orderMaster.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        foreach (PropertyInfo item0 in properties0)
                        {
                            string name = item0.Name;
                            object value = item0.GetValue(orderMaster, null);

                            foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                            {
                                if (String.Compare(fld.RunningDBField, name, true) == 0)
                                {
                                    fld.Value = value;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion

                    #region 主表Payment与Grid++Report记录集的字段之间的对应关系
                    if (orderPayment != null)
                    {
                        PropertyInfo[] properties2 = orderPayment.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        foreach (PropertyInfo item2 in properties2)
                        {
                            string name = item2.Name;
                            object value = item2.GetValue(orderPayment, null);

                            foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                            {
                                if (String.Compare(fld.RunningDBField, name, true) == 0)
                                {
                                    fld.Value = value;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion

                    #region 主表takeoutdetail配送信息与Grid++Report记录集的字段之间的对应关系
                    if (takeoutdetail != null)
                    {
                        PropertyInfo[] properties3 = takeoutdetail.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        foreach (PropertyInfo item3 in properties3)
                        {
                            string name = item3.Name;
                            object value = item3.GetValue(takeoutdetail, null);
                            foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                            {
                                if (String.Compare(fld.RunningDBField, name, true) == 0)
                                {
                                    fld.Value = value;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion

                    Report.DetailGrid.Recordset.Post();
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        //在C#中一次填入一条记录不能成功，只能使用一次将记录全部填充完的方式
        private void ReportFetchRecord_SubPresent()
        {
            //获取子报表的内部报表对象  
            try
            {
                SubPresent.DetailGrid.Recordset.Empty();
                if ((taskdetails == null) || (taskdetails.Count == 0) || (!orderMaster.isPresent))
                {
                    Report.ControlByName("SubPresent").Visible = false;
                    return;
                }
                var taskdetail1 = taskdetails.First();
                PropertyInfo[] properties1 = taskdetail1.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                var dataDetails = taskdetails.Where(p => p.dataType == (int)GlobalEnumVars.OrderingDataType.Present).ToList();
                if ((dataDetails == null) || (dataDetails.Count == 0)) return;
                foreach (var taskdetail in dataDetails)
                {
                    SubPresent.DetailGrid.Recordset.Append();

                    #region 菜品明细与Grid++Report记录集的字段之间的对应关系
                    foreach (PropertyInfo item1 in properties1)
                    {
                        string name = item1.Name;
                        object value = item1.GetValue(taskdetail, null);

                        foreach (IGRField fld in SubPresent.DetailGrid.Recordset.Fields)
                        {
                            if (String.Compare(fld.RunningDBField, name, true) == 0)
                            {
                                fld.Value = value;
                                break;
                            }
                        }
                    }
                    #endregion                    

                    #region 主表Master与Grid++Report记录集的字段之间的对应关系
                    if (orderMaster != null)
                    {
                        orderMaster.deskName = taskdetail.deskName;
                        PropertyInfo[] properties0 = orderMaster.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        foreach (PropertyInfo item0 in properties0)
                        {
                            string name = item0.Name;
                            object value = item0.GetValue(orderMaster, null);

                            foreach (IGRField fld in SubPresent.DetailGrid.Recordset.Fields)
                            {
                                if (String.Compare(fld.RunningDBField, name, true) == 0)
                                {
                                    fld.Value = value;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion                    

                    SubPresent.DetailGrid.Recordset.Post();
                }
            }
            catch { }
        }

        //在C#中一次填入一条记录不能成功，只能使用一次将记录全部填充完的方式
        private void ReportFetchRecord_SubRptPay()
        {
            //获取子报表的内部报表对象  
            try
            {
                SubPayment.DetailGrid.Recordset.Empty();
                if ((paymentList == null) || (paymentList.Count == 0)) return;
                var paymentInfo = paymentList.First();
                PropertyInfo[] properties1 = paymentInfo.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (var payment in paymentList)
                {
                    SubPayment.DetailGrid.Recordset.Append();

                    #region 菜品明细与Grid++Report记录集的字段之间的对应关系
                    foreach (PropertyInfo item1 in properties1)
                    {
                        string name = item1.Name;
                        object value = item1.GetValue(payment, null);

                        foreach (IGRField fld in SubPayment.DetailGrid.Recordset.Fields)
                        {
                            if (String.Compare(fld.RunningDBField, name, true) == 0)
                            {
                                fld.Value = value;
                                break;
                            }
                        }
                    }
                    #endregion

                    #region 主表Master与Grid++Report记录集的字段之间的对应关系
                    if (orderMaster != null)
                    {
                        PropertyInfo[] properties0 = orderMaster.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        foreach (PropertyInfo item0 in properties0)
                        {
                            string name = item0.Name;
                            object value = item0.GetValue(orderMaster, null);

                            foreach (IGRField fld in SubPayment.DetailGrid.Recordset.Fields)
                            {
                                if (String.Compare(fld.RunningDBField, name, true) == 0)
                                {
                                    fld.Value = value;
                                    break;
                                }
                            }
                        }
                    }
                    #endregion

                    SubPayment.DetailGrid.Recordset.Post();
                }
            }
            catch
            { }
        }

        //在C#中一次填入一条记录不能成功，只能使用一次将记录全部填充完的方式
        private void ReportFetchRecord_SubFoodDepart()
        {
            //获取子报表的内部报表对象  
            try
            {
                SubFoodDepart.DetailGrid.Recordset.Empty();
                if ((departList == null) || (departList.Count == 0)) return;
                var foodTypeInfo = departList.First();
                PropertyInfo[] properties1 = foodTypeInfo.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (var foodType in departList)
                {
                    SubFoodDepart.DetailGrid.Recordset.Append();

                    #region 分类明细与Grid++Report记录集的字段之间的对应关系
                    foreach (PropertyInfo item1 in properties1)
                    {
                        string name = item1.Name;
                        object value = item1.GetValue(foodType, null);

                        foreach (IGRField fld in SubFoodDepart.DetailGrid.Recordset.Fields)
                        {
                            if (String.Compare(fld.RunningDBField, name, true) == 0)
                            {
                                fld.Value = value;
                                break;
                            }
                        }
                    }
                    #endregion                    

                    SubFoodDepart.DetailGrid.Recordset.Post();
                }
            }
            catch
            { }
        }

        /// <summary>
        /// 厨房打印
        /// </summary>
        /// <param name="_taskdetails"></param>
        /// <param name="isPrintPreview"></param>
        public void SetPrinKitchenReport(corecmsprintscheme printscheme, bool isPrintPreview = false)
        {
            int printStatus = (int)GlobalEnumVars.PrintStatus.Treat;
            try
            {
                //var billtemplate = printscheme.billtemplates.FirstOrDefault();
                var schemedetail = printscheme.printschemedetails.Find(p => p.billtypeId == (int)GlobalEnumVars.PrintbillType.Kitchen);
                var billtemplate = schemedetail.billtemplate;

                if ((billtemplate == null) || (string.IsNullOrEmpty(billtemplate.reportFile))) return;

                if (billtemplate.reportData.IsNullOrEmpty())
                {
                    string sFile = System.Windows.Forms.Application.StartupPath + "\\report\\" + billtemplate.reportFile;
                    Report.LoadFromFile(sFile);
                }
                else
                {
                    Report.LoadFromStr(billtemplate.reportData);
                }

                if (!string.IsNullOrEmpty(billtemplate.printerName) && (billtemplate.printerName != "默认打印机"))
                    Report.Printer.PrinterName = billtemplate.printerName;

                Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
                if (isPrintPreview)
                {
                    Report.PrintPreview(true);
                }
                else
                {
                    Report.Print(false);
                    printStatus = (int)GlobalEnumVars.PrintStatus.Success;
                }
            }
            catch
            {
                printStatus = (int)GlobalEnumVars.PrintStatus.Fail;
            }

            foreach (var taskdetail in taskdetails)
            {
                taskdetail.printStatus = printStatus;
                taskdetail.updataTime = DateTime.Now;
            }
            new corecmsprinttaskdetailServices().UpdateAsync(taskdetails);
        }

        /// <summary>
        /// 厨房打印
        /// </summary>
        /// <param name="_taskdetails"></param>
        /// <param name="isPrintPreview"></param>
        public void SetPrinKitchen(List<corecmsprinttaskdetail> _taskdetails, corecmsprintscheme printscheme, bool isPrintPreview = false)
        {
            var dataDetail = _taskdetails.First();
            var schemedetail = printscheme.printschemedetails.Find(p => p.billtypeId == dataDetail.billtypeId);
            if (schemedetail == null) return;
            var billtemplate = schemedetail.billtemplate;
            if (billtemplate == null) return;

            if ((billtemplate.foodPrintType == (int)GlobalEnumVars.foodPrintType.Order)
            || (billtemplate.foodPrintType == (int)GlobalEnumVars.foodPrintType.Summary))
            {
                taskdetails = _taskdetails;
                SetPrinKitchenReport(printscheme, isPrintPreview);
            }
            else
            {
                foreach (var detail in _taskdetails)
                {
                    taskdetails.Clear();
                    taskdetails.Add(detail);
                    SetPrinKitchenReport(printscheme, isPrintPreview);
                }
            }
        }

        /// <summary>
        /// 预结算单（汇总打印)
        /// </summary>        
        /// <param name="taskdetails"></param>
        public void SetPrintPrePayment(corecmsfoodorderingmaster _orderMaster, corecmsprintbilltemplate billtemplate, bool isPrintPreview = false)
        {
            string strMaster = JsonConvert.SerializeObject(_orderMaster);
            orderMaster = JsonConvert.DeserializeObject<corecmsfoodordermaster>(strMaster);
            orderMaster.isPresent = true;
            taskdetails = new corecmsprinttaskdetailServices().QueryFromOrdering(_orderMaster, billtemplate);

            departList = new corecmsfoodorderingServices().GetOrderFoodDeparts(_orderMaster.orderId);
            var sysUser = new sysuserServices().GetById(orderMaster.sysUserId);
            if (sysUser != null)
            {
                orderMaster.sysUserName = sysUser.nickName;
                orderMaster.sysUserNumber = sysUser.userName;
            }
            if (orderMaster.userId > 0)
                corecmsuser = new corecmsuserServices().GetById(orderMaster.userId);

            if (billtemplate.reportData.IsNullOrEmpty())
            {
                string sFile = System.Windows.Forms.Application.StartupPath + "\\report\\" + billtemplate.reportFile;
                Report.LoadFromFile(sFile);
            }
            else
            {
                Report.LoadFromStr(billtemplate.reportData);
            }

            if (!string.IsNullOrEmpty(billtemplate.printerName) && (billtemplate.printerName != "默认打印机"))
                Report.Printer.PrinterName = billtemplate.printerName;

            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            try
            {
                SubFoodDepart = Report.ControlByName("SubFoodDepart").AsSubReport.Report;
                SubFoodDepart.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord_SubFoodDepart);
            }
            catch { }
            try
            {
                SubPresent = Report.ControlByName("SubPresent").AsSubReport.Report;
                SubPresent.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord_SubPresent);
            }
            catch { }
            if (isPrintPreview)
            {
                Report.PrintPreview(true);
            }
            else
            {
                if (billtemplate.printCount <= 0) billtemplate.printCount = 1;
                for (int i = 1; i <= billtemplate.printCount; i++)
                {
                    Report.Print(false);
                }
            }
        }

        /// <summary>
        /// 结账单打印（汇总打印)
        /// </summary>        
        /// <param name="taskdetails"></param>
        public void SetPrintSettlement(corecmsfoodordermaster _orderMaster, corecmsprintbilltemplate billtemplate, bool isPrintPreview = false)
        {
            orderMaster = _orderMaster;
            taskdetails = new corecmsprinttaskdetailServices().QueryFromOrderDetail(_orderMaster, billtemplate);

            //处理全部是赠送的
            if (taskdetails.Count == taskdetails.Select(p => p.dataType == (int)GlobalEnumVars.OrderingDataType.Present).Count())
            {
                orderMaster.isPresent = false;
            }

            orderPayment = new corecmsfoodorderpaymentServices().QueryOrderPayment(_orderMaster.orderId, (int)GlobalEnumVars.Payment.cash, ref paymentList);
            var sysUser = new sysuserServices().GetById(_orderMaster.sysUserId);
            if (sysUser != null)
            {
                _orderMaster.sysUserName = sysUser.nickName;
                _orderMaster.sysUserNumber = sysUser.userName;
            }
            if (orderMaster.sourceType == 2)
                takeoutdetail = new corecmstakeoutdetailServices().QueryById(_orderMaster.orderId);

            //会员信息.
            if (orderMaster.userId > 0)
            {
                corecmsuser = new corecmsuserServices().GetById(orderMaster.userId);
                if (corecmsuser != null) corecmsuser.userNumber = corecmsuser.number;
                if (!orderMaster.isUpload.ObjToBool())
                    corecmsuser.currentPoint = corecmsuser.point.ObjToInt(0) + orderMaster.userPoint.ObjToInt(0);
                else
                    corecmsuser.currentPoint = corecmsuser.point.ObjToInt(0);
            }
            if (billtemplate.reportData.IsNullOrEmpty())
            {
                string sFile = System.Windows.Forms.Application.StartupPath + "\\report\\" + billtemplate.reportFile;
                Report.LoadFromFile(sFile);
            }
            else
            {
                Report.LoadFromStr(billtemplate.reportData);
            }

            if (!string.IsNullOrEmpty(billtemplate.printerName) && (billtemplate.printerName != "默认打印机"))
                Report.Printer.PrinterName = billtemplate.printerName;

            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);

            try
            {
                SubPresent = Report.ControlByName("SubPresent").AsSubReport.Report;
                SubPresent.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord_SubPresent);
            }
            catch { }

            try
            {
                SubPayment = Report.ControlByName("SubPayment").AsSubReport.Report;
                SubPayment.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord_SubRptPay);
            }
            catch { }

            if (isPrintPreview)
            {
                Report.PrintPreview(true);
            }
            else
            {
                if (billtemplate.printCount <= 0) billtemplate.printCount = 1;
                for (int i = 1; i <= billtemplate.printCount; i++)
                {
                    Report.Print(false);
                }
            }
        }

        /// <summary>
        /// 标签打印
        /// </summary>        
        /// <param name="taskdetails"></param>
        public void SetPrintLabel(corecmsfoodordermaster _orderMaster, bool isPrintPreview = false)
        {
            orderMaster = _orderMaster;
            orderMaster.isPresent = false;
            //开始发送打印
            var sysUser = new sysuserServices().GetById(_orderMaster.sysUserId);
            if (sysUser != null)
            {
                _orderMaster.sysUserName = sysUser.nickName;
                _orderMaster.sysUserNumber = sysUser.userName;
            }

            //会员信息.
            if (orderMaster.userId > 0)
                corecmsuser = new corecmsuserServices().GetById(orderMaster.userId);

            var labelTaskDetails = new corecmsprinttaskdetailServices().QueryFromOrderLabel(_orderMaster);

            //拆分成打印记录
            foreach (var item in labelTaskDetails)
            {
                var billtemplate = item.labeltemplate;
                if (billtemplate == null) continue;
                if (billtemplate.foodPrintType == (int)GlobalEnumVars.foodPrintType.Number)
                {
                    for (int i = 0; i < item.quantity; i++)
                    {
                        item.sumQuantity = item.quantity;
                        item.sumAmount = item.amount;
                        item.sumAllAmount = item.realAmount;
                        item.quantity = 1;
                        item.amount = item.price;
                        item.allAmount = item.realAmount;
                        item.pageName = string.Format("{0}/{1}", i + 1, item.quantity.ToString("#"));
                        SetPrintLabelItem(item, billtemplate, isPrintPreview);
                    }
                }
                else
                {
                    item.pageName = "";
                    SetPrintLabelItem(item, billtemplate, isPrintPreview);
                }
            }
        }

        /// <summary>
        /// 标签打印
        /// </summary>        
        /// <param name="taskdetails"></param>
        public void SetPrintLabelItem(corecmsprinttaskdetail item, corecmsprintbilltemplate billtemplate, bool isPrintPreview = false)
        {
            if (billtemplate.reportData.IsNullOrEmpty())
            {
                string sFile = System.Windows.Forms.Application.StartupPath + "\\report\\" + billtemplate.reportFile;
                Report.LoadFromFile(sFile);
            }
            else
            {
                Report.LoadFromStr(billtemplate.reportData);
            }

            if (!string.IsNullOrEmpty(billtemplate.printerName) && (billtemplate.printerName != "默认打印机"))
                Report.Printer.PrinterName = billtemplate.printerName;

            taskdetails.Clear();
            taskdetails.Add(item);
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            if (isPrintPreview)
            {
                Report.PrintPreview(true);
            }
            else
            {
                Report.Print(false);
            }
        }

        /// <summary>
        /// 客户点菜单（原单打印)-----
        /// </summary>
        /// <param name="_taskdetails"></param>
        /// <param name="billtemplate"></param>
        /// <param name="isPrintPreview"></param>
        public void SetPrinOrdering(List<corecmsprinttaskdetail> _taskdetails
            , corecmsprintbilltemplate billtemplate, corecmsfoodorderingmaster orderingmaster
            , bool isPrintPreview = false)
        {
            taskdetails = _taskdetails;
            if ((taskdetails == null) || (taskdetails.Count == 0)) return;

            var taskdetail = taskdetails.First();
            string strOrderingmaster = JsonConvert.SerializeObject(orderingmaster);
            orderMaster = JsonConvert.DeserializeObject<corecmsfoodordermaster>(strOrderingmaster);
            int rowId = 1;
            foreach (var item in taskdetails)
            {
                item.rowId = rowId;
                rowId++;
            }
            var sysUser = new sysuserServices().GetById(orderingmaster.sysUserId);
            if (sysUser != null)
            {
                orderMaster.sysUserName = sysUser.nickName;
                orderMaster.sysUserNumber = sysUser.userName;
            }
            if (billtemplate.reportData.IsNullOrEmpty())
            {
                string sFile = System.Windows.Forms.Application.StartupPath + "\\report\\" + billtemplate.reportFile;
                Report.LoadFromFile(sFile);
            }
            else
            {
                Report.LoadFromStr(billtemplate.reportData);
            }

            if (!string.IsNullOrEmpty(billtemplate.printerName) && (billtemplate.printerName != "默认打印机"))
                Report.Printer.PrinterName = billtemplate.printerName;

            if (billtemplate.printCount <= 0) billtemplate.printCount = 1;

            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            if (isPrintPreview)
            {
                Report.PrintPreview(true);
            }
            else
            {
                for (int i = 1; i <= billtemplate.printCount; i++)
                {
                    Report.Print(false);
                }
            }
        }

        /// <summary>
        /// 小票格式设置
        /// </summary>
        /// <param name="printbilltemplate"></param>
        public void SetDesignReport(corecmsprintbilltemplate printbilltemplate)
        {
            GridppReport Report = new GridppReport();
            if (string.IsNullOrEmpty(printbilltemplate.reportFile))
            {
                var printbilltype = new corecmsprintbilltypeServices().GetById(printbilltemplate.billtypeId);
                printbilltemplate.reportFile = PrinterUnits.GetReportFileName(printbilltype, (int)PageSize.W80);
                new corecmsprintbilltemplateServices().UpdateAsync(printbilltemplate);
            }
            if (printbilltemplate.reportData.IsNullOrEmpty())
            {
                string sFile = System.Windows.Forms.Application.StartupPath + "\\Report\\" + printbilltemplate.reportFile;
                Report.LoadFromFile(sFile);
            }
            else
            {
                Report.LoadFromStr(printbilltemplate.reportData);
            }
            //frmDesignForm theForm = new frmDesignForm();
            ////theForm.AttachReport(Report, sFile);
            //theForm.AttachReport(Report);
            //theForm.ShowDialog();
            //if (!string.IsNullOrEmpty(theForm.sReportData))
            //{
            //    printbilltemplate.reportData = theForm.sReportData;
            //    new corecmsprintbilltemplateServices().UpdateAsync(printbilltemplate);
            //}
            //theForm.Dispose();

            frmReportDesigner theForm = new frmReportDesigner();
            theForm.AttachReport(Report, printbilltemplate);
            theForm.ShowDialog();
            theForm.Dispose();
        }
    }
}
